package cn.edu.csust.coursecard.controller;

import cn.edu.csust.coursecard.annotation.RequiredAdmin;
import cn.edu.csust.coursecard.dao.dto.*;
import cn.edu.csust.coursecard.dao.dto.Message;
import cn.edu.csust.coursecard.dao.entity.*;
import cn.edu.csust.coursecard.common.CodeEnum;
import cn.edu.csust.coursecard.common.ReturnData;
import cn.edu.csust.coursecard.exception.BaseException;
import cn.edu.csust.coursecard.service.ICommentService;
import cn.edu.csust.coursecard.service.IPostService;
import cn.edu.csust.coursecard.service.PostReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 论坛视图层
 */
@RestController
public class ForumController extends BaseController{
    
    @Autowired
    private IPostService postService;
    
    @Autowired
    private ICommentService commentService;

    @Autowired
    private PostReportService postReportService;
    
    @PostMapping("/post/write")
    public ReturnData writePost(Post post, List<MultipartFile> images, HttpServletRequest request){
        Integer userId = getUserIdFromRequest(request);
        post.setUserId(userId);
        IndexPost indexPost = postService.write(post,images);
        return ReturnData.success(indexPost);
    }
    
    @GetMapping("/theme/all")
    public ReturnData getTheme(){
        List<Theme> themeList = postService.getTheme();
        return ReturnData.success(themeList);
    }
    
    @PostMapping("/theme/add")
    @RequiredAdmin
    public ReturnData addTheme(@RequestBody Theme theme){
        postService.addTheme(theme);
        return ReturnData.success();
    }
    
    @GetMapping("/theme/delete")
    @RequiredAdmin
    public ReturnData deleteTheme(@RequestParam("id") Integer themeId){
        postService.deleteTheme(themeId);
        return ReturnData.success();
    }
    
    @PostMapping("/post/index")
    public ReturnData forumIndex(@RequestBody PostSearchVo postVo, HttpServletRequest request){
        Integer userId = getUserIdFromRequest(request);
        Map<String,Object> map = postService.getIndexPost(postVo,userId);
        return ReturnData.success(map);
    }
    
    @GetMapping("/post/detail")
    public ReturnData postDetail(@RequestParam("id") Integer postId,HttpServletRequest request){
        PostDetail postDetail = postService.getDetailPost(postId, getUserIdFromRequest(request));
        return ReturnData.success(postDetail);
    }
    
    @GetMapping("/post/like")
    public ReturnData likePost(@RequestParam("id") Integer postId,HttpServletRequest request){
        Integer userId = getUserIdFromRequest(request);
        postService.likePost(postId,userId);
        return ReturnData.success();
    }
    
    @GetMapping("/post/enshrine")
    public ReturnData enshrinePost(@RequestParam("id") Integer postId,HttpServletRequest request){
        postService.enshrinePost(getUserIdFromRequest(request),postId);
        return ReturnData.success();
    }

    @GetMapping("/enshrine/list")
    public ReturnData getEnshrinePost(HttpServletRequest request){
        Integer userId = getUserIdFromRequest(request);
        List<IndexPost> posts = postService.getEnshrinePost(userId);
        return ReturnData.success(posts);
    }
    
    @GetMapping("/post/self")
    public ReturnData getSelfPost(HttpServletRequest request){
        Integer userId = getUserIdFromRequest(request);
        List<IndexPost> posts = postService.getSelfPost(userId);
        return ReturnData.success(posts);
    }
    
    @PostMapping("/comment/write")
    public ReturnData writeComment(@RequestBody Comment comment, HttpServletRequest request){
        Integer usrId = getUserIdFromRequest(request);
        comment.setUserId(usrId);
        CommentInfo commentInfo = commentService.writeComment(comment);
        return ReturnData.success(commentInfo);
    }
    
    
    @PostMapping("/reply/write")
    public ReturnData writeReply(@RequestBody Reply reply, HttpServletRequest request){
        reply.setUserId(getUserIdFromRequest(request));
        ReplyInfo replyInfo = commentService.writeReply(reply);
        return ReturnData.success(replyInfo);
    }
    
    @PostMapping("/post/delete")
    public ReturnData delete(@RequestBody PostDeleteVo postDeleteVo, HttpServletRequest request){
        Integer userId = getUserIdFromRequest(request);
        Boolean isAdmin = getAdminFromRequest(request);
        if(postDeleteVo.getPostId()!=null){
            //删除帖子
            IndexPost post = postService.getIndexPostByPrimaryKey(postDeleteVo.getPostId());
            authCheck(isAdmin,userId,post.getUserInfo().getUserId());
            postService.deletePost(postDeleteVo.getPostId());
        }else if(postDeleteVo.getCommentId() != null){
            Comment comment = commentService.getCommentByPrimaryKey(postDeleteVo.getCommentId());
            authCheck(isAdmin,userId,comment.getUserId());
            commentService.deleteComment(comment.getPostId(),Collections.singletonList(postDeleteVo.getCommentId()));
        }else if(postDeleteVo.getReplyId() != null){
            //删除回复
            Reply reply = commentService.getReplyByPrimaryKey(postDeleteVo.getReplyId());
            authCheck(isAdmin,userId,reply.getUserId());
            commentService.deleteReply(Collections.singletonList(postDeleteVo.getReplyId()));
        }
        return ReturnData.success();
    }
    
    private void authCheck(Boolean isAdmin,Integer requestUserId,Integer mysqlUserId){
        if(isAdmin == null && !mysqlUserId.equals(requestUserId)){
            throw new BaseException(CodeEnum.AUTH_ERROR.getCode(), "权限错误");
        }
    }
    
    @GetMapping("/message/unread")
    public ReturnData unread(HttpServletRequest request){
        Integer userId = getUserIdFromRequest(request);
        List<Message> messages = commentService.getUnreadMessage(userId);
        return ReturnData.success(messages);
    }
    
    @GetMapping("/message/hasRead")
    public  ReturnData hasRead(HttpServletRequest request){
        Integer userId = getUserIdFromRequest(request);
        List<Message> messages = commentService.getHasReadMessage(userId);
        return ReturnData.success(messages);
    }
    
    
    @PostMapping("/message/setRead")
    public ReturnData setMessageToRead(@RequestBody Message message){
        commentService.setMessageToRead(message);
        return ReturnData.success();
    }
    
    @PostMapping("/message/setAllRead")
    public ReturnData setAllMessageToRead(HttpServletRequest request){
        commentService.setAllMessageToRead(getUserIdFromRequest(request));
        return ReturnData.success();
    }

    @PostMapping("/report/add")
    public ReturnData reportPost(Integer postId,HttpServletRequest request){
        postReportService.addPostReport(getUserIdFromRequest(request),postId);
        return ReturnData.success();
    }

    @GetMapping("/report/cancel")
    public ReturnData cancelReport(Integer postId,HttpServletRequest request){
        postReportService.deletePostReport(getUserIdFromRequest(request),postId);
        return ReturnData.success();
    }

    @GetMapping("/report/getAll")
    @RequiredAdmin
    public ReturnData getAllReport(){
        return ReturnData.success(postReportService.selectPostReportDetail());
    }
}
